/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#pragma once

#include "basic_def.h"
#include "basic_vec.h"






Inline int32 f_JIT_单值整数计算(int32 v1, int32 v2, int32 CalType) {
	int32 r;
	switch (CalType) {
		case 0: r = int32(v1) + int32(v2); break;
		case 1: r = int32(v1) - int32(v2); break;
		case 2: r = int32(v1) * int32(v2); break;
		case 3: r = int32(v1) / int32(DEF_Max(v2,1)); break;

		case 4: r = DEF_Max(v1, v2); break;
		case 5: r = DEF_Min(v1, v2); break;
		case 6: r = int32(v1) % int32(DEF_Max(v2, 1)); break;
	}
	return r;
}

template<typename T1, typename T2>
Inline float32 f_JIT_Fun_f_c_f(T1 v1, T2 v2, int32 CalType) {
	float32 r;
	switch (CalType) {
		case 0: r = float32(v1) + float32(v2); break;
		case 1: r = float32(v1) - float32(v2); break;
		case 2: r = float32(v1) * float32(v2); break;
		case 3: r = float32(v1) / float32(v2 + 0.0000000001); break;

		case 4: r = DEF_Max(v1, v2); break;
		case 5: r = DEF_Min(v1, v2); break;
		case 6: r = int32(v1) % int32(DEF_Max(v2, 1)); break;
	}
	return r;
}





Inline int64 f_JIT_坐标转索引(const ivec3& 坐标, const ivec3& 分辨率, uint8 CalType) {
	int64 r{};
	return (分辨率.x * 分辨率.z) * 坐标.y + 分辨率.x * 坐标.z + 坐标.x;

	//switch (CalType) {
	//	case 0 : {
	//		return (分辨率.y * 分辨率.z) * 坐标.x + 分辨率.y * 坐标.z + 坐标.y;
	//		//return (分辨率.y * 分辨率.z) * 坐标.x + 分辨率.x * 坐标.z + 坐标.y;
	//	}
	//	case 1: {
	//		return (分辨率.x * 分辨率.z) * 坐标.y + 分辨率.x * 坐标.z + 坐标.x;
	//	}
	//	case 2: {
	//		return (分辨率.x * 分辨率.y) * 坐标.z + 分辨率.x * 坐标.y + 坐标.x;
	//	}
	//}
	//return r;
}

Inline void f_JIT_坐标转索引m(const S_iVec3Array* 坐标, const S_iVec3Array* 分辨率, S_UI32Array* 索引, uint8 CalType) {
	uint32 num = DEF_Max(坐标->count, 分辨率->count);

	f_core_array_resize((S_Array*)索引, num);

	bool 坐标多例 = 坐标->count >= num;
	bool 分辨率多例 = 分辨率->count >= num;

	for (uint32 i = 0; i < num; ++i) {
		uint32 index = f_JIT_坐标转索引(坐标多例 ? 坐标->ptr_userData[i] : 坐标->ptr_userData[0], 分辨率多例 ? 分辨率->ptr_userData[i] : 分辨率->ptr_userData[0], CalType);
		索引->ptr_userData[i] = index;
	}
}

Inline ivec3 f_JIT_索引转坐标(const uint32 index, ivec3 分辨率, uint8 CalType) {
	ivec3 r;
	分辨率.z = DEF_Max(分辨率.z, 1);
	uint32 层元素数量 = DEF_Max(分辨率.x, 1) * 分辨率.z;
	r.y = index / 层元素数量;
	层元素数量 *= r.y;

	r.z = (index - 层元素数量) / 分辨率.x;
	r.x = index - (层元素数量 + r.z * 分辨率.x);
	return r;

	//switch (CalType) {
	//	case 0: {
	//		分辨率.y = DEF_Max(分辨率.y, 1);
	//		uint32 层元素数量 = 分辨率.z * 分辨率.y;
	//		r.x = index / DEF_Max(层元素数量,1);
	//		层元素数量 *= r.x;
	//
	//		r.z = (index - 层元素数量) / 分辨率.y;
	//		r.y = index - (层元素数量 + r.z * 分辨率.y);
	//		break;
	//	}
	//	case 1: {
	//		分辨率.z = DEF_Max(分辨率.z, 1);
	//		uint32 层元素数量 = DEF_Max(分辨率.x,1) * 分辨率.z;
	//		r.y = index / 层元素数量;
	//		层元素数量 *= r.y;
	//
	//		r.z = (index - 层元素数量) / 分辨率.x;
	//		r.x = index - (层元素数量 + r.z * 分辨率.x);
	//		break;
	//	}
	//	case 2: {
	//		分辨率.y = DEF_Max(分辨率.y, 1);
	//		uint32 层元素数量 = DEF_Max(分辨率.x, 1) * 分辨率.y;
	//		r.z = index / 层元素数量;
	//		层元素数量 *= r.z;
	//
	//		r.y = (index - 层元素数量) / 分辨率.z;
	//		r.x = index - (层元素数量 + r.y * 分辨率.x);
	//		break;
	//	}
	//}
	//return r;
}

Inline void f_JIT_索引转坐标m(const S_UI32Array* 索引, const S_iVec3Array* 分辨率, S_iVec3Array* 坐标, uint8 CalType) {
	uint32 num = DEF_Max(索引->count, 分辨率->count);

	f_core_array_resize((S_Array*)坐标, num);

	bool 坐标多例 = 索引->count >= num;
	bool 分辨率多例 = 分辨率->count >= num;

	for (uint32 i = 0; i < num; ++i) {
		ivec3 c = f_JIT_索引转坐标(坐标多例 ? 索引->ptr_userData[i] : 索引->ptr_userData[0], 分辨率多例 ? 分辨率->ptr_userData[i] : 分辨率->ptr_userData[0], CalType);
		坐标->ptr_userData[i] = c;
	}
}




















